package com.kyivmohyla.kma2.client.presenter.manager;

import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.kyivmohyla.kma2.client.UserService;
import com.kyivmohyla.kma2.client.events.manager.ManagerCloseOrderEvent;
import com.kyivmohyla.kma2.client.gin.Injector;
import com.kyivmohyla.kma2.client.presenter.Presenter;
import com.kyivmohyla.kma2.client.utils.Dialog;
import com.kyivmohyla.kma2.client.view.common.OrderCellTableWidget;
import com.kyivmohyla.kma2.client.view.manager.ClosedOrderView;
import com.kyivmohyla.kma2.shared.model.UIOrder;

import java.util.Date;
import java.util.List;

public class ManagerOpenClosedOrdersPresenter implements Presenter {

    private final Display display;
    private final Injector injector = Injector.INSTANCE;
    private MultiSelectionModel<UIOrder> multiSelectionModel;
    private CellTable<UIOrder> orderCellTable;

    public ManagerOpenClosedOrdersPresenter() {
        this.display = injector.getManagerClosedOrdersView();
        this.orderCellTable = (CellTable) this.display.getOrderCellTable().getOrderCellTable();
        createTableStructure();
        bind();
        UserService.App.getInstance().getOrdersByState(new int[]{6}, new AsyncCallback<List<UIOrder>>() {
            @Override
            public void onFailure(Throwable throwable) {
                Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
            }

            @Override
            public void onSuccess(List<UIOrder> uiOrders) {
                ListDataProvider<UIOrder> dataProvider = new ListDataProvider<UIOrder>(uiOrders);
                dataProvider.addDataDisplay(orderCellTable);
            }
        });
    }

    private void bind() {
        this.display.getBackButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                injector.getEventBus().fireEvent(new ManagerCloseOrderEvent(new Date()));
            }
        });
    }

    private void createTableStructure() {
        multiSelectionModel = new MultiSelectionModel<UIOrder>();
        orderCellTable.setSelectionModel(multiSelectionModel);

        orderCellTable.addDomHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                ClosedOrderView closedOrderView = new ClosedOrderView((UIOrder) multiSelectionModel.getSelectedSet().toArray()[0]);
                closedOrderView.setGlassEnabled(true);
                closedOrderView.center();
                closedOrderView.show();
            }
        }, DoubleClickEvent.getType());

        final DateTimeFormat dateFormat = DateTimeFormat.getFormat("dd.MM.yyyy HH:mm");
        orderCellTable.addColumn(new Column<UIOrder, Number>(new NumberCell()) {
            @Override
            public Number getValue(UIOrder object) {
                return object.getOrderId();
            }
        }, "ID");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                return object.getStateName();
            }
        }, "State");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                if (object.getCreateDate() != null)
                    return dateFormat.format(object.getCreateDate());
                return "#######";
            }
        }, "Creation Date");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                return object.getCustomerName();
            }
        }, "Customer");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                if (object.getDesStartDate() != null)
                    return dateFormat.format(object.getDesStartDate());
                return "#######";
            }
        }, "Desirable Start");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                if (object.getDesEndDate() != null)
                    return dateFormat.format(object.getDesEndDate());
                return "#######";
            }
        }, "Desirable End");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                return object.getCommentary();
            }
        }, "Commentary");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                if (object.getStartDate() != null)
                    return dateFormat.format(object.getStartDate());
                return "#######";
            }
        }, "Planned Start");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                if (object.getEndDate() != null)
                    return dateFormat.format(object.getEndDate());
                return "#######";
            }
        }, "Planned End");
        orderCellTable.addColumn(new Column<UIOrder, String>(new TextCell()) {
            @Override
            public String getValue(UIOrder object) {
                return object.getEngineerName();
            }
        }, "Engineer");
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    public interface Display {

        HasClickHandlers getBackButton();

        OrderCellTableWidget getOrderCellTable();

        Widget asWidget();
    }
}
